package com.example.chupachups;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;

public class YouTubeActivity extends YouTubeBaseActivity implements
View.OnClickListener,
CompoundButton.OnCheckedChangeListener,
YouTubePlayer.OnFullscreenListener, OnInitializedListener, PlayerStateChangeListener {
	

	private static final String TAG = "YouTubeActivity";
	private YouTubePlayerView playerView;
	private YouTubePlayer player;
	private Intent intent;
	private String videoid;
	private String message;
	private String videoRequestUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		intent = getIntent();
		videoid = intent.getStringExtra("videoid");
		message = intent.getStringExtra("message");
		setVideoRequestUrl(intent.getStringExtra("videoRequestUrl"));
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		setContentView(R.layout.activity_you_tube);
		
		playerView = (YouTubePlayerView) findViewById(R.id.player);
        playerView.initialize(DeveloperKey.DEVELOPER_KEY, this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_you_tube, menu);
		return true;
	}
	
 

	 @Override
	 public void onFullscreen(boolean isFullscreen) {
	
	 }


	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onInitializationFailure(Provider arg0,
			YouTubeInitializationResult arg1) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	  public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);
	   // doLayout();
	  }


	@Override
	public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
		this.player = player;
		player.setPlayerStateChangeListener(this);
	    player.loadVideo(videoid);
	}

	@Override
	public void onAdStarted() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onError(ErrorReason arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoaded(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoading() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onVideoEnded() {
		/*Intent myIntent = new Intent(YouTubeActivity.this, MessageActivity.class);
		myIntent.putExtra("message", message);*/
		
		Intent myIntent = new Intent(YouTubeActivity.this, MainActivity.class);
    	//http://chupa-test.appspot.com/retrievevid1

		if(getVideoRequestUrl().equals("http://chupa-test.appspot.com/retrievevid1")){
			setVideoRequestUrl("http://chupa-test.appspot.com/retrievevid2");
		}else{
			setVideoRequestUrl("http://chupa-test.appspot.com/retrievevid1");
		}
		myIntent.putExtra("videoRequestUrl", getVideoRequestUrl());
		YouTubeActivity.this.startActivity(myIntent);
		
	}

	@Override
	public void onVideoStarted() {
		
		
		// TODO Auto-generated method stub
		
	}

	public String getVideoRequestUrl() {
		return videoRequestUrl;
	}

	public void setVideoRequestUrl(String newVideoRequestUrl) {
		this.videoRequestUrl = newVideoRequestUrl;
	}

}
